#include "BoundedMailBox.h"

BoundedMailBox::BoundedMailBox(int size):
	m_maxLength(0),
	m_empty(size-1),
	m_full(0)
{

}

BoundedMailBox::~BoundedMailBox()
{

}

bool BoundedMailBox::isEmpty()
{
	return mailbox.empty();
}

int BoundedMailBox::PutIn(Message* m)
{
	m_empty.down();
	mut.lock(self());
	//printf("Pushing... %s\n", m->text());
	mailbox.push(m);
	if (mailbox.size()> m_maxLength)
		m_maxLength=mailbox.size();
	mut.unlock();
	m_full.up();
	//return 0;
}

Message* BoundedMailBox::Extract()
{
	m_full.down();
	mut.lock(self());
	Message* msg=(Message*)mailbox.front();
	//printf("Extracting... %s\n", msg->text());
	mailbox.pop();
	m_empty.up();
	mut.unlock();
	return msg;
}

int BoundedMailBox::getMaxSize()
{
	return m_maxLength;
}

int BoundedMailBox::getSize()
{
	return mailbox.size();
}

void BoundedMailBox::cleanMailBox()
{
	while (!mailbox.empty())
	{
		mailbox.pop();
	}
	m_maxLength=0;
}

void BoundedMailBox::setSize(int size)
{
	m_empty.setValue(size);
}	






